package com.bos.service.impl;

import com.bos.dao.repositories.MenuRepository;
import com.bos.domian.system.Menu;
import com.bos.domian.system.User;
import com.bos.service.MenuService;
import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.List;

/**
 * @author misterWei
 * @create 2019年01月01号:20点39分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional(rollbackFor = {NullPointerException.class, ParseException.class,NumberFormatException.class})
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuRepository menuRepository;
    @Override
    public List<Menu> findMenuAll() {
        //根据优先级查询
        Sort sort = new Sort(Sort.Direction.ASC,"priority");
        return menuRepository.findAll(sort);
    }

    @Override
    public void add(Menu model) {
         //判断当前是否有关联

        //如果没有关联进行操作
        if (model.getParentMenu() !=null && model.getParentMenu().getId() == 0) {
          model.setParentMenu(null);
        }
        menuRepository.save(model);

    }

    @Override
    public List<Menu> findByUser(User user) {
        //测试用户为 admin 如果是admin那么就是测试数据
        if ("admin".equalsIgnoreCase(user.getUsername())){
           return menuRepository.findAll();
        }else {
            return menuRepository.findByUser(user.getId());
        }

    }
}
